﻿' NOTE: You can use the "Rename" command on the context menu to change the class name "ReportGenerator" in code, svc and config file together.
Imports CrystalReportHelperLib
Imports System.IO

Public Class ReportGenerator
    Implements IReportGenerator

    Public Function GeneratePDF(ByVal cro As CRObject) As CRResponseObject Implements IReportGenerator.GeneratePDF
        Dim ResponseObject As New CRResponseObject

        Dim ExportFileName As String = ""
        Try

            Dim CReport = New CrystalReportHelper()

            ExportFileName = GenerateExportFileName(cro.ExportPath.Trim, cro.ExportFileName, cro.IncludeTimestampOnFile)

            Using CReport


                CReport.InitialiseCrystalReport(cro.FileReportPath)

                If cro.ServerName IsNot Nothing AndAlso cro.ServerName.Trim.Length > 0 Then
                    CReport.SetDataSourceConnection(cro.ServerName.Trim, cro.DBName.Trim, cro.UserName.Trim, cro.PW.Trim)
                End If

                If cro.VariableList IsNot Nothing Then
                    For i As Integer = 0 To cro.VariableList.Count - 1
                        CReport.SetParameter(cro.VariableList(i).VariableName, cro.VariableList(i).VariableValue)
                    Next
                End If

                CReport.ExportCrystalReport("PortableDocFormat", ExportFileName)
                CReport = Nothing

                ResponseObject.ExportedFileName = ExportFileName
                ResponseObject.StatusInfo = StatusInfoType.Success
            End Using
        Catch ex As Exception
            Dim ErrList As New List(Of String)
            ResponseObject.ErrorInfo.Add(ex.Message)
            If ex.InnerException.Message IsNot Nothing Then

                ResponseObject.ErrorInfo.Add(ex.InnerException.Message)
            End If
            ResponseObject.ErrorInfo = ErrList
        End Try

        Return ResponseObject
    End Function

    Private Function GenerateExportFileName(ByVal ExportFolder As String, ByVal ExportFileName As String, ByVal IncludeTimestampOnFile As Boolean) As String
        Dim TS As String = String.Format("{0:yyyyMMddhhmmss}", Now)
        If (ExportFolder.Substring(ExportFolder.Length - 1) <> "\") Then
            ExportFolder = ExportFolder & "\"
        End If

        If Not Directory.Exists(ExportFolder) Then
            Directory.CreateDirectory(ExportFolder)
        End If
        Dim TempFileName As String = ExportFileName
        Dim ID As Integer = ExportFileName.LastIndexOf(".")
        If ID > 0 Then
            TempFileName = ExportFileName.Substring(0, ID)
            If IncludeTimestampOnFile Then
                TempFileName = ExportFolder & TempFileName & "_" & TS & ".pdf"
            Else
                TempFileName = ExportFolder & TempFileName & ".pdf"
            End If

        Else
            TempFileName = ExportFolder & TempFileName
        End If

        Return TempFileName
    End Function
End Class
